<template>
  <div>
    <el-dialog
      :title="form._id ? '修改' : '新增'"
      :visible.sync="dialogFormVisible"
      width="400px"
    >
      <el-form :model="form" ref="form" :rules="rules" label-width="100px">
        <el-form-item label="商品名称" prop="name">
          <el-input type="text" v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="品牌" prop="brandId">
          <el-input type="text" v-model="form.brandId"></el-input>
        </el-form-item>
        <el-form-item label="轮播图图片" prop="imgs">
          <upload
            action="/upload"
            :cicle="true"
            :width="300"
            :height="300"
            :on-success="onSuccess"
          >
            <img
              style="max-width: 100%; max-height: 100%"
              v-if="form.imgs"
              :src="form.imgs"
              class="avatar"
            />
            <div
              v-else
              style="
                border: 1px dashed #e4e4e4;
                width: 100px;
                height: 100px;
                line-height: 100px;
                text-align: center;
              "
            >
              <i class="el-icon-plus avatar-uploader-icon"></i>
            </div>
          </upload>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input type="text" v-model="form.description"></el-input>
        </el-form-item>
        <el-form-item label="规格" prop="specification">
          <el-input type="text" v-model="form.specification"></el-input>
        </el-form-item>
        <el-form-item label="详细" prop="content">
          <el-input type="text" v-model="form.content"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false" size="small"
          >取 消</el-button
        >
        <el-button @click="submit" type="primary" size="small">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import api from '@/api'
import upload from "@/components/Upload";
export default {
  name: "spEdit",
  components: {
    upload,
  },
  beforeRouteEnter(to, from, next) {
    // 在渲染该组件的对应路由被验证前调用
    // 不能获取组件实例 `this` ！
    // 因为当守卫执行时，组件实例还没被创建！
    //监听整个页面流程
    console.log("组件内守卫");
    next();
  },
  data() {
    return {
      dialogFormVisible: false,
      form: {
        _id: 0,
        name:"",
        imgs: "",
        brandId:"",
        description: "",
        specification: "",
        content:""
      },
      rules: {
        brandId: [{ required: true, message: "品牌不能为空", trigger: "blur" }],
        name: [{ required: true, message: "商品名称不能为空", trigger: "blur" }],
      },
    };
  },
  methods: {
    //图片上传
    changeFile(e) {
      let files = e.target.files;
      let formData = new FormData();
      formData.append("file", files[0]);

      this.$http({
        url: "/upload",
        method: "POST",
        data: formData,
      }).then((res) => {
        if (res.path)
          this.form.imgs =
            api.baseURL + "/" + res.path.replace(/uploads/, "uploads/");
        console.log(res);
      });
    },
    openFile(){
      let fileElement =this.$refs.file
      fileElement.click()
    },
    handleOpen(key, keyPath) {
      console.log(key, keyPath);
    },
    handleClose(key, keyPath) {
      console.log(key, keyPath);
    },
    ///新增
    add() {
      ///初始化
      this.form = {
        _id: 0,
        name: "",
        imgs: "",
        brandId: "",
        description:"",
        specificatio:"",
        content:""
      };
      this.dialogFormVisible = true;
    },
    //修改
    edit(row) {
      this.form = row;
      this.dialogFormVisible = true;
    },
    submit() {
      //校验
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.form.indexId = this.form._id;
          this.$http({
            url: "/admin/dp",
            method: this.form.indexId ? "PUT" : "POST", ///通过indexId判断新增还是修改
            data: this.form,
          }).then((res) => {
            if (!res.errorMsg) {
              this.dialogFormVisible = false;
              this.$emit("load", this.form.indexId ? undefined : 1); //在子组键触发访问父组件方法
              // this.getList(this.form.indexId ? undefined : 1);
            }
          });
        }
      });
    },
    onSuccess(res) {
      console.log(res);
      //拼装图片路径赋值给表单
      this.form.imgs = api.baseURL + "/uploads/" + res.filename;
    },
  },
  created() {},
  mounted() {},
};

</script>
